<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Model\Order;
use App\Model\Customer;
use App\Model\Route;
use App\Model\Biaoqian;
use App\Model\Deal;
use App\Model\Bumen;
use App\Model\User;
class XiadanController extends Controller
{
    // 调用下单页面
    public function index()
    { 
        $type=Route::where('parent_id',0)->get();
        // $didian=Route::where('parent_id',Route::where('parent_id',0)->first()->id)->get();
        $biaoqian=Biaoqian::all();
        return view('order.sousuo',['type'=>$type,'biaoqian'=>$biaoqian]);
    }
    // 表单筛选查询
    public function create()
    {
        return view('order.sousuo');
    }
    // 执行搜索
    public function sousuo(Request $request)
    {
        $list=json_decode($request['data']);
        if(session('wechat.role')){
            foreach(session('wechat.role') as $vo){
                if($vo=="总经理"){
                    $order=Customer::all();
                }elseif($vo=="经理"){
                    $bumen=session('wechat.userid');
                    $bumen=\DB::table('admin_user_permissions')->where('user_id',$bumen)->first()->permission_id;
                    $bumen=\DB::table('admin_user_permissions')->where('permission_id',$bumen)->pluck('user_id');
                    $order=Customer::whereIn('customer_weihu',$bumen)->get();
                }else{
                    $order=Customer::where('customer_weihu',session('wechat.userid'))->get();
                }
                if($list->customer){
                    $customer_id=\DB::table("crm_customer")->where('customer_name','like','%'.$list->customer.'%')->pluck("id");
                    $order=$order->whereIn('id',$customer_id);
                }
                if($list->phone){
                    $order=$order->where('customer_phone',$list->phone);
                }
                if($list->number){
                    $order=$order->where('customer_sfz',$list->number);
                }
                return json_encode($order);
            }
        }else{
            return json_encode('false');
        }
    }
    // 执行工单添加
    public function tianjia(Request $request)
    {
        $list=json_decode($request['data']);
        $data=[];
        $data['order_customerid']=$list->{'order_customerid'};
        $data['order_didian']=Route::where('title',$list->{'order_didian'})->first()->id;
        $data['order_didian2']=Route::where('title',$list->{'order_didian2'})->first()->id;
        $data['order_xianlu']=$list->{'order_xianlu'};
        $data['order_lvyoudate']=$list->{'order_lvyoudate'};
        $data['order_lvyouleibie']=Biaoqian::where('name',$list->{'order_lvyouleibie'})->first()->id;
        $data['order_beizhu']=$list->{'order_beizhu'};
        $data['order_orderdate']=date("y-m-d h:i:s");
        if(session('wechat.userid')){
            $data['staff_id']=session('wechat.userid');
        }else{
            return redirect(url('/'));
            die();
        }
        $aaa=Order::firstOrCreate($data);
        if($aaa){
            return json_encode($aaa);
        }else{
            return json_encode('false');
        }
    }
    // 执行客户添加
    public function customer(Request $request)
    {
        $list=json_decode($request['data']);
        $data=[];
        $laiyuan=['1'=>'网站','2'=>'转介绍','3'=>'上门','4'=>'电话销售','5'=>'老客户','6'=>'其他'];
        $laiyuan=array_keys($laiyuan,$list->{'customer_laiyuan'});
        $data['customer_laiyuan']=$laiyuan[0];
        $data['customer_name']=$list->{'customer_name'};
        $data['customer_sex']=$list->{'customer_sex'};
        $data['customer_danwei']=$list->{'customer_danwei'};
        $data['customer_phone']=$list->{'customer_phone'};
        $data['customer_weixin']=$list->{'customer_weixin'};
        $data['customer_qq']=$list->{'customer_qq'};
        $data['customer_email']=$list->{'customer_email'};
        $data['customer_sfz']=$list->{'customer_sfz'};
        if($list->{'customer_sfz'}!=''){
            $data['customer_shengri']=substr($list->{'customer_sfz'},-8,4);
        }
        $data['customer_beizhu']=$list->{'customer_beizhu'};
        if($list->{'customer_age'}){
            $data['customer_age']=$list->{'customer_age'};
        }else{
            $data['customer_age']=0;
        }
        $data['customer_type']=$list->{'customer_type'};
        $data['customer_kfdate']=date("y-m-d h:i:s");
        if(session('wechat.userid')){
            $user=\DB::table('admin_users')->where('id',session('wechat.userid'))->first();
            $data['customer_weihu']=$user->id;
            $data['yuangong']=$user->username;
        }else{
            return redirect(url('/'));
            die();
        }
        if($data['customer_phone'] !=''){
            $phone=Customer::where('customer_phone',$data['customer_phone'])->first();
            if($phone){
                return json_encode('phone');
            }else{
                $aaa=Customer::create($data);
            }
        }else{
            $aaa=Customer::create($data);
        }
        if($aaa){
            return json_encode($aaa);
        }else{
            return json_encode('false');
        }
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
